package com.community.web.controller.community;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.community.body.domain.ZyRoom;
import com.community.body.service.IZyRoomService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.enums.BusinessType;
import com.community.common.utils.poi.ExcelUtil;
import com.community.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 房屋信息Controller
 *
 * @author community
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/community/room")
public class ZyRoomController extends BaseController {
    @Autowired
    private IZyRoomService zyRoomService;

    /**
     * 级联
     */
    @PreAuthorize("@ss.hasPermi('community:room:cascade')")
    @GetMapping("/cascade")
    public AjaxResult  cascade() {
        return success(zyRoomService.selectZyRoomCascade());
    }


    /**
     * 查询房屋信息列表
     */
    @PreAuthorize("@ss.hasPermi('community:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZyRoom zyRoom) {
        startPage();
        List<ZyRoom> list = zyRoomService.selectZyRoomList(zyRoom);
        return getDataTable(list);
    }

    /**
     * 导出房屋信息列表
     */
    @PreAuthorize("@ss.hasPermi('community:room:export')")
    @Log(title = "房屋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZyRoom zyRoom) {
        String[] split = zyRoom.getRoomCode().split(",");

        Long[] longArray = Arrays.stream(split).map(Long::valueOf).toArray(Long[]::new);
        List<ZyRoom> list = zyRoomService.selectZyRoomExport(longArray);
        ExcelUtil<ZyRoom> util = new ExcelUtil<ZyRoom>(ZyRoom.class);
        util.exportExcel(response, list, "房屋信息数据");
    }

    /**
     * 导入房屋信息
     */
    @PreAuthorize("@ss.hasPermi('community:room:importData')")
    @Log(title = "楼栋管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws IOException {
        ExcelUtil<ZyRoom> util = new ExcelUtil<>(ZyRoom.class);
        List<ZyRoom> buildingList = util.importExcel(file.getInputStream());

        String message = zyRoomService.importData(buildingList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 下载模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ZyRoom> util = new ExcelUtil<ZyRoom>(ZyRoom.class);
        util.importTemplateExcel(response, "楼栋信息导入模板");
    }

    /**
     * 获取房屋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:room:query')")
    @GetMapping(value = "/{roomId}")
    public AjaxResult getInfo(@PathVariable("roomId") Long roomId) {
        return success(zyRoomService.selectZyRoomByRoomId(roomId));
    }

    /**
     * 新增房屋信息
     */
    @PreAuthorize("@ss.hasPermi('community:room:add')")
    @Log(title = "房屋信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyRoom zyRoom) {
        // 校验唯一房间
        int i = zyRoomService.selectCheck(zyRoom);
        if(i > 0){
            return AjaxResult.error("房间号已存在");
        }
        return toAjax(zyRoomService.insertZyRoom(zyRoom));
    }

    /**
     * 修改房屋信息
     */
    @PreAuthorize("@ss.hasPermi('community:room:edit')")
    @Log(title = "房屋信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyRoom zyRoom) {
        return toAjax(zyRoomService.updateZyRoom(zyRoom));
    }

    /**
     * 删除房屋信息
     */
    @PreAuthorize("@ss.hasPermi('community:room:remove')")
    @Log(title = "房屋信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roomIds}")
    public AjaxResult remove(@PathVariable Long[] roomIds) {
        return toAjax(zyRoomService.deleteZyRoomByRoomIds(roomIds));
    }
}
